package org.elte.lipika.controllers;

import org.elte.lipika.Occurence;
import org.elte.lipika.Terminus;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.support.FormattingConversionServiceFactoryBean;
import org.springframework.roo.addon.web.mvc.controller.RooConversionService;

/**
 * A central place to register application converters and formatters. 
 */
@RooConversionService
public class ApplicationConversionServiceFactoryBean extends FormattingConversionServiceFactoryBean {

	@Override
	protected void installFormatters(FormatterRegistry registry) {
		registry.addConverter(new TerminusConverter());
		registry.addConverter(new OccurenceConverter());
		super.installFormatters(registry);
		
		// Register application converters and formatters
	}
	
	/*public Converter<Terminus, String> TerminusConverter(){
		return new Converter<Terminus, String>() {
            public String convert(Terminus source) {
                
            }
        };
	}
*/	
	
	 static class OccurenceConverter implements Converter<Occurence, String> {
	        public String convert(Occurence occurence) {
	            return new StringBuilder().append(occurence.getSource().getAuthor()).append(":").append(occurence.getSource().getTitle()).append("; ").append(occurence.getPage()).toString();
	        }
	        
	    }
	 
	 static class TerminusConverter implements Converter<Terminus, String> {
	        public String convert(Terminus terminus) {
	        	return new StringBuilder().append(terminus.getOrigLatin().getOriginalText()).append(" - ").append(terminus.getInterpretation().getOriginalText()).toString();
	        }
	        
	    }
	
}
